function message(data) {
    alert(data);
}
var sid = "";

function logSuc(sessionId) {
    sid = sessionId;
    dialog = new Array();
    unreadMsgBox = new Array();
    timer = setTimeout('getNewMsg()', 1000);
    updateUserInfo();
    go("index.html");
}

function go(url) {
    $("#mainFrame").attr("src", url);
}

function logout() {
    $.post(getUrl(), {
        service: "logout",
        sessionId: sid
    },
    function(data) {
    });
    sid = "";
    clearTimeout(timer);
    go("login.html");
}

var friendList = null;
function getFriendList(callback) {
    $.post(getUrl(), {
        service: "friendList",
        sessionId: sid
    },
    function(data) {
        var jo = JSON.parse(data);
        if (jo.state == "success") {
            friendList = jo.data;
            callback(jo.data);
        } else {
            errorDisposer(jo.error);
        }
    });
}

var userInfo;
function updateUserInfo() {
    $.post(getUrl(), {
        service: "userInfo",
        sessionId: sid,
    },
    function(data) {
        jo = JSON.parse(data);
        if (jo.state == "success") {
            userInfo = jo.data;
        } else {
            errorDisposer(jo.error);
        }
    });
}

var unReadMsgBox = new Array();
function getNewMsg() {
    $.post(getUrl(), {
        service: "getMsg",
        sessionId: sid
    },
    function(data) {
        var jo = JSON.parse(data);
        if (jo.state == "success") {
            if (! (jo.data.msg[0] === undefined)) {
                $.each(jo.data.msg,
                function(k, v) {
                	var bool = false;
                	$.each(dialog,function(kk,vv){
                		if(vv.id == v.sender){
                			if(vv.diadiv!=null){
                				if($('#mainFrame').attr('src')=='index.html'){
                					vv.msgs.push(v);
                					window.frames['mainFrame'].loadLog(vv,$(vv.diadiv));
                					bool = true;
                				}
                			}
                		}
                		
                	})
                	if(bool == false){
                		unReadMsgBox.push(v);
                	}
                });
            }
            timer = setTimeout('getNewMsg()', 3000);
        } else {
            errorDisposer(jo.error);
        }
    });
}
var timer;

function sendMsg(msgData, callback) {

    $.post(getUrl(), {
        service: "sendMsg",
        reciver: msgData.reciver,
        body: msgData.body
    },
    function(data) {
        var jo = JSON.parse(data);
        if (jo.state == "success") {
            callback(msgData);
        } else {
            errorDisposer(jo.error);
        }

    });

}

function errorDisposer(error) {
    $.each(error,
    function(k, v) {
        if (v.code == 2) {
            sid = "";
            clearTimeout(timer);
            alert('登录超时');
            go('login.html');
        }
    });
}

var dialog = new Array();

function openDialog(uid, nickName, callback) {
    var dia = null;
    // 判断是否是已经创建过的dialog
    $.each(dialog,
    function(k, v) {
        if (v.id == uid) {
            dia = v;
        }
    });

    if (dia == null) {
        // 如果没有已经打开的dialog
        if (nickName == null) {
            // 获取用户nickName
            $.post(getUrl(), {
                service: "friendInfo",
                sessionId: sid,
                user: uid
            },
            function(data) {
                var jo = JSON.parse(data);
                if (jo.state == 'success') {
                    openDialog(uid, jo.data.nickName, callback);
                } else {
                    errorDisposer(jo.error);
                }
            });
            return;
        } else {
            dia = new Object();
            dia.id = uid;
            dia.nickName = nickName;
            dia.msgs = new Array();
            dia.opened = false;
            dia.diadiv = null;
            // 加入窗体列表
            dialog.push(dia);
        }
    }

    // 将未读消息中对应该id的消息加入
    $.each(unReadMsgBox,
    function(k,v) {
        if (v.sender == uid) {
            unReadMsgBox.splice(k);
            dia.msgs.push(v);
        }
    });
    // 处理完成，回调
    if(callback!=null){
    	callback(dia);
    }
}

function dealUnread(callback){
	$.each(unReadMsgBox,function(k,v){
		openDialog(v.sender,null,callback);
	});
}